<!DOCTYPE html>
<!-- saved from url=(0045)http://mybatis.org/spring/zh/factorybean.html -->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="zh" lang="zh"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="author" content="Hunter Presnall">
    <meta name="author" content="Eduardo Macarron">
    <meta name="author" content="Nan Lei">
    <meta name="Date-Revision-yyyymmdd" content="20200309">
    <meta http-equiv="Content-Language" content="zh">
    <title>mybatis-spring – MyBatis-Spring | SqlSessionFactoryBean</title>
    <link rel="stylesheet" href="./mybatis-spring – MyBatis-Spring _ SqlSessionFactoryBean_files/apache-maven-fluido-1.7.min.css">
    <link rel="stylesheet" href="./mybatis-spring – MyBatis-Spring _ SqlSessionFactoryBean_files/site.css">
    <link rel="stylesheet" href="./mybatis-spring – MyBatis-Spring _ SqlSessionFactoryBean_files/print.css" media="print">
    <script type="text/javascript" src="./mybatis-spring – MyBatis-Spring _ SqlSessionFactoryBean_files/apache-maven-fluido-1.7.min.js.下载"></script>
  </head>
  <body class="topBarDisabled">
    <div class="container-fluid">
      <div id="banner">
        <div class="pull-left"><div id="bannerLeft"><h2>mybatis-spring</h2>
</div>
</div>
        <div class="pull-right"><a href="http://mybatis.org/" id="bannerRight" title="MyBatis logo"><img src="./mybatis-spring – MyBatis-Spring _ SqlSessionFactoryBean_files/mybatis-logo.png" alt="MyBatis logo"></a></div>
        <div class="clear"><hr></div>
      </div>

      <div id="breadcrumbs">
        <ul class="breadcrumb">
        <li id="publishDate">最近更新: 09 三月 2020<span class="divider">|</span>
</li>
          <li id="projectVersion">版本: 2.0.4</li>
        </ul>
      </div>
      <div class="row-fluid">
        <div id="leftColumn" class="span2">
          <div class="well sidebar-nav">
    <ul class="nav nav-list">
      <li class="nav-header">参考文档</li>
    <li><a href="http://mybatis.org/spring/zh/index.html" title="简介"><span class="none"></span>简介</a></li>
    <li><a href="http://mybatis.org/spring/zh/getting-started.html" title="入门"><span class="none"></span>入门</a></li>
    <li class="active"><a href="http://mybatis.org/spring/zh/factorybean.html#"><span class="none"></span>SqlSessionFactoryBean</a></li>
    <li><a href="http://mybatis.org/spring/zh/transactions.html" title="事务"><span class="icon-chevron-right"></span>事务</a></li>
    <li><a href="http://mybatis.org/spring/zh/sqlsession.html" title="使用 SqlSession"><span class="icon-chevron-right"></span>使用 SqlSession</a></li>
    <li><a href="http://mybatis.org/spring/zh/mappers.html" title="注入映射器"><span class="icon-chevron-right"></span>注入映射器</a></li>
    <li><a href="http://mybatis.org/spring/zh/boot.html" title="使用 Spring Boot"><span class="none"></span>使用 Spring Boot</a></li>
    <li><a href="http://mybatis.org/spring/zh/using-api.html" title="使用 MyBatis API"><span class="none"></span>使用 MyBatis API</a></li>
    <li><a href="http://mybatis.org/spring/zh/batch.html" title="使用 Spring Batch"><span class="none"></span>使用 Spring Batch</a></li>
    <li><a href="http://mybatis.org/spring/zh/sample.html" title="示例代码"><span class="none"></span>示例代码</a></li>
      <li class="nav-header">项目文档</li>
    <li><a href="http://mybatis.org/spring/zh/project-info.html" title="项目信息"><span class="icon-chevron-right"></span>项目信息</a></li>
    <li><a href="http://mybatis.org/spring/zh/project-reports.html" title="项目报表"><span class="icon-chevron-right"></span>项目报表</a></li>
</ul>
          <hr>
          <div id="poweredBy">
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="clear"></div>
            <div class="clear"></div>
<a href="http://maven.apache.org/" title="构建依靠 Maven" class="poweredBy"><img class="builtBy" alt="构建依靠 Maven" src="./mybatis-spring – MyBatis-Spring _ SqlSessionFactoryBean_files/maven-feather.png"></a>
            </div>
          </div>
        </div>
        <div id="bodyColumn" class="span10">


  
    <div class="section">
<h2><a name="SqlSessionFactoryBean"></a>SqlSessionFactoryBean</h2>
      
<p>
      在基础的 MyBatis 用法中，是通过 <tt>SqlSessionFactoryBuilder</tt> 来创建 <tt>SqlSessionFactory</tt> 的。
      而在 MyBatis-Spring 中，则使用 <tt>SqlSessionFactoryBean</tt> 来创建。
      </p>
      
    
<div class="section">
<h3><a name="a.E8.AE.BE.E7.BD.AE"></a>设置</h3>
      
<p>
      要创建工厂 bean，将下面的代码放到 Spring 的 XML 配置文件中：
      </p>
        
<div class="source"><pre class="prettyprint"><span class="tag">&lt;bean</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"sqlSessionFactory"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"org.mybatis.spring.SqlSessionFactoryBean"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="atn">ref</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/bean&gt;</span></pre></div>


<p>
  需要注意的是 <tt>SqlSessionFactoryBean</tt> 实现了 Spring 的 <tt>FactoryBean</tt> 接口（参见 Spring 官方文档 3.8 节 <a class="externalLink" href="https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#beans-factory-extension-factorybean">通过工厂 bean 自定义实例化逻辑</a>）。这意味着由 Spring 最终创建的 bean <b>并不是</b> <tt>SqlSessionFactoryBean</tt> 本身，而是工厂类（<tt>SqlSessionFactoryBean</tt>）的 getObject() 方法的返回结果。这种情况下，Spring 将会在应用启动时为你创建 <tt>SqlSessionFactory</tt>，并使用 <tt>sqlSessionFactory</tt> 这个名字存储起来。
</p>

<p>
  等效的 Java 代码如下：
</p>

      
<div class="source"><pre class="prettyprint"><span class="lit">@Bean</span><span class="pln">
</span><span class="kwd">public</span><span class="pln"> </span><span class="typ">SqlSessionFactory</span><span class="pln"> sqlSessionFactory</span><span class="pun">()</span><span class="pln"> </span><span class="pun">{</span><span class="pln">
  </span><span class="typ">SqlSessionFactoryBean</span><span class="pln"> factoryBean </span><span class="pun">=</span><span class="pln"> </span><span class="kwd">new</span><span class="pln"> </span><span class="typ">SqlSessionFactoryBean</span><span class="pun">();</span><span class="pln">
  factoryBean</span><span class="pun">.</span><span class="pln">setDataSource</span><span class="pun">(</span><span class="pln">dataSource</span><span class="pun">());</span><span class="pln">
  </span><span class="kwd">return</span><span class="pln"> factoryBean</span><span class="pun">.</span><span class="pln">getObject</span><span class="pun">();</span><span class="pln">
</span><span class="pun">}</span></pre></div>

      
<p>
        通常，在 MyBatis-Spring 中，你不需要直接使用 <tt>SqlSessionFactoryBean</tt> 或对应的 <tt>SqlSessionFactory</tt>。相反，session 的工厂 bean 将会被注入到 <tt>MapperFactoryBean</tt> 或其它继承于 <tt>SqlSessionDaoSupport</tt> 的 DAO（Data Access Object，数据访问对象）中。
      </p>
    </div>

    
<div class="section">
<h3><a name="a.E5.B1.9E.E6.80.A7"></a>属性</h3>

      
<p>
        <tt>SqlSessionFactory</tt> 有一个唯一的必要属性：用于 JDBC 的 <tt>DataSource</tt>。这可以是任意的 <tt>DataSource</tt> 对象，它的配置方法和其它 Spring 数据库连接是一样的。
      </p>

      
<p>
        一个常用的属性是 <tt>configLocation</tt>，它用来指定 MyBatis 的 XML 配置文件路径。它在需要修改 MyBatis 的基础配置非常有用。通常，基础配置指的是 <tt>&lt;settings&gt;</tt> 或 <tt>&lt;typeAliases&gt;</tt> 元素。
      </p>

      
<p>
        需要注意的是，这个配置文件<b>并不需要</b>是一个完整的 MyBatis 配置。确切地说，任何环境配置（<tt>&lt;environments&gt;</tt>），数据源（<tt>&lt;DataSource&gt;</tt>）和 MyBatis 的事务管理器（<tt>&lt;transactionManager&gt;</tt>）都会被<b>忽略</b>。<tt>SqlSessionFactoryBean</tt> 会创建它自有的 MyBatis 环境配置（<tt>Environment</tt>），并按要求设置自定义环境的值。
      </p>

      
<p>
        如果 MyBatis 在映射器类对应的路径下找不到与之相对应的映射器 XML 文件，那么也需要配置文件。这时有两种解决办法：第一种是手动在 MyBatis 的 XML 配置文件中的 &lt;mappers&gt; 部分中指定 XML 文件的类路径；第二种是设置工厂 bean 的 <tt>mapperLocations</tt> 属性。
      </p>

      
<p>
        <tt>mapperLocations</tt> 属性接受多个资源位置。这个属性可以用来指定 MyBatis 的映射器 XML 配置文件的位置。属性的值是一个 Ant 风格的字符串，可以指定加载一个目录中的所有文件，或者从一个目录开始递归搜索所有目录。比如:
      </p>
      
        
<div class="source"><pre class="prettyprint"><span class="tag">&lt;bean</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"sqlSessionFactory"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"org.mybatis.spring.SqlSessionFactoryBean"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="atn">ref</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"mapperLocations"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"classpath*:sample/config/mappers/**/*.xml"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/bean&gt;</span></pre></div>

      
<p>
        这会从类路径下加载所有在 sample.config.mappers 包和它的子包中的 MyBatis 映射器 XML 配置文件。
      </p>

      
<p>
        在容器管理事务的时候，你可能需要的一个属性是 <tt>transactionFactoryClass</tt>。请参考事务一章的相关章节。
      </p>

      
<p>
        如果你使用了多个数据库，那么需要设置 <tt>databaseIdProvider</tt> 属性：
      </p>

      
<div class="source"><pre class="prettyprint"><span class="tag">&lt;bean</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"databaseIdProvider"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"org.apache.ibatis.mapping.VendorDatabaseIdProvider"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"properties"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;props&gt;</span><span class="pln">
      </span><span class="tag">&lt;prop</span><span class="pln"> </span><span class="atn">key</span><span class="pun">=</span><span class="atv">"SQL Server"</span><span class="tag">&gt;</span><span class="pln">sqlserver</span><span class="tag">&lt;/prop&gt;</span><span class="pln">
      </span><span class="tag">&lt;prop</span><span class="pln"> </span><span class="atn">key</span><span class="pun">=</span><span class="atv">"DB2"</span><span class="tag">&gt;</span><span class="pln">db2</span><span class="tag">&lt;/prop&gt;</span><span class="pln">
      </span><span class="tag">&lt;prop</span><span class="pln"> </span><span class="atn">key</span><span class="pun">=</span><span class="atv">"Oracle"</span><span class="tag">&gt;</span><span class="pln">oracle</span><span class="tag">&lt;/prop&gt;</span><span class="pln">
      </span><span class="tag">&lt;prop</span><span class="pln"> </span><span class="atn">key</span><span class="pun">=</span><span class="atv">"MySQL"</span><span class="tag">&gt;</span><span class="pln">mysql</span><span class="tag">&lt;/prop&gt;</span><span class="pln">
    </span><span class="tag">&lt;/props&gt;</span><span class="pln">
  </span><span class="tag">&lt;/property&gt;</span><span class="pln">
</span><span class="tag">&lt;/bean&gt;</span><span class="pln">

</span><span class="tag">&lt;bean</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"sqlSessionFactory"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"org.mybatis.spring.SqlSessionFactoryBean"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="atn">ref</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"mapperLocations"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"classpath*:sample/config/mappers/**/*.xml"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"databaseIdProvider"</span><span class="pln"> </span><span class="atn">ref</span><span class="pun">=</span><span class="atv">"databaseIdProvider"</span><span class="tag">/&gt;</span><span class="pln">
</span><span class="tag">&lt;/bean&gt;</span></pre></div>

        
<p>
          <span class="label important">提示</span> 自 1.3.0 版本开始，新增的 <tt>configuration</tt> 属性能够在没有对应的 MyBatis XML 配置文件的情况下，直接设置 <tt>Configuration</tt> 实例。例如：
        </p>

        
<div class="source"><pre class="prettyprint"><span class="tag">&lt;bean</span><span class="pln"> </span><span class="atn">id</span><span class="pun">=</span><span class="atv">"sqlSessionFactory"</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"org.mybatis.spring.SqlSessionFactoryBean"</span><span class="tag">&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="atn">ref</span><span class="pun">=</span><span class="atv">"dataSource"</span><span class="pln"> </span><span class="tag">/&gt;</span><span class="pln">
  </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"configuration"</span><span class="tag">&gt;</span><span class="pln">
    </span><span class="tag">&lt;bean</span><span class="pln"> </span><span class="atn">class</span><span class="pun">=</span><span class="atv">"org.apache.ibatis.session.Configuration"</span><span class="tag">&gt;</span><span class="pln">
      </span><span class="tag">&lt;property</span><span class="pln"> </span><span class="atn">name</span><span class="pun">=</span><span class="atv">"mapUnderscoreToCamelCase"</span><span class="pln"> </span><span class="atn">value</span><span class="pun">=</span><span class="atv">"true"</span><span class="tag">/&gt;</span><span class="pln">
    </span><span class="tag">&lt;/bean&gt;</span><span class="pln">
  </span><span class="tag">&lt;/property&gt;</span><span class="pln">
</span><span class="tag">&lt;/bean&gt;</span></pre></div>

      </div>
    </div>
  

        </div>
      </div>
    </div>
    <hr>
    <footer>
      <div class="container-fluid">
        <div class="row-fluid">
            <p>Copyright ©2010–2020
<a href="http://www.mybatis.org/">MyBatis.org</a>.
All rights reserved.</p>
        </div>
      </div>
    </footer>
  

<div class="xl-chrome-ext-bar" id="xl_chrome_ext_{4DB361DE-01F7-4376-B494-639E489D19ED}" style="display: none;">
      <div class="xl-chrome-ext-bar__logo"></div>

      <a id="xl_chrome_ext_download" href="javascript:;" class="xl-chrome-ext-bar__option">下载视频</a>
      <a id="xl_chrome_ext_close" href="javascript:;" class="xl-chrome-ext-bar__close"></a>
    </div></body></html>